package com.butuh.uang.tuh.ang.util;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Build;
import android.os.ParcelUuid;
import android.util.Base64;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.UUID;

import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.log.XLog;

public class RandomUtil {


    /**
     * 获取设备唯一标识UUID
     * @param mContext
     * @return
     */
    public static String getUUID(Context mContext) {
        String identity;
        try {
            identity = (String) SPUtils.get(mContext, "identity", "");
            if (identity.equals("")) {
                identity = java.util.UUID.randomUUID().toString();
                SPUtils.put(mContext, "identity", identity);
            }
        } catch (Exception e) {
            identity = "";
        }
        return identity;
    }


    /**
     * 得到全局唯一UUID,无权限时通过UUID.randomUUID().toString()随机产生一个UUID
     */
    public static String getUUID1() {
        return UUID.randomUUID().toString();
    }


    public static String getRand() {
        String degis = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        return Kits.Random.getRandom(degis, 32);
    }


    //    使用方法按照getSecurityAppKey方法使用，将要加密的字串写到encryptToSHA中即可！
    public static String encryptToSHA(String info) {
        byte[] digesta = null;
        try {
            MessageDigest alga = MessageDigest.getInstance("SHA-1");
            alga.update(info.getBytes());
            digesta = alga.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        String rs = byte2hex(digesta);
        return rs;
    }

    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs;
    }


    public static String base64encodeToString(Context context) {
        String str = "googlePlay:" + context.getPackageName() + "(" + Kits.Package.getVersionName(context) + "<" + Kits.Package.getVersionCode(context) + ">)";
//        打包渠道名:包名(版本名称 <版本号>)
        XLog.e("==str===" + str);
        String baseStr = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
        XLog.e("==baseStr===" + baseStr);
        baseStr = baseStr.replaceAll("\\+", "-").replaceAll("/", "_").replaceAll("=", "");
        XLog.e("==baseSt1r===" + baseStr);
        return baseStr;
    }


}
